home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  2.1 KB  |  115 lines

  1. /* getopt.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: getopt.c,v 4.7 1995/02/08 13:14:56 espie Exp $
  6.  * $Log: getopt.c,v $
  7.  * Revision 4.7  1995/02/08  13:14:56  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.7  1995/02/08  13:14:56  espie
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 4.6  1995/02/01  20:41:45  espie
  14.  * Added color.
  15.  *
  16.  * Revision 4.6  1995/02/01  20:41:45  espie
  17.  * Added color.
  18.  *
  19.  * Revision 4.5  1995/02/01  16:39:04  espie
  20.  * Moved includes to defs.h
  21.  *
  22.  * Revision 4.5  1995/02/01  16:39:04  espie
  23.  * Moved includes to defs.h
  24.  *
  25.  * Revision 1.5  1993/12/04  16:12:50  espie
  26.  * New getopt semantics.
  27.  */
  28.  
  29. #include <ctype.h>
  30.  
  31. #include "defs.h"
  32. #include "getopt.h"
  33.  
  34. ID("$Id: getopt.c,v 4.7 1995/02/08 13:14:56 espie Exp $")
  35.  
  36. int optind = 1;
  37. char *optarg = 0;
  38. LOCAL not_an_option = 0;
  39.  
  40. LOCAL int parse_option(argv, option)
  41. char *argv[];
  42. struct long_option *option;
  43.     {
  44.     optind++;
  45.     if (option->argn)
  46.         optarg = argv[optind++];
  47.     return option->code;
  48.     }
  49.  
  50. int getlongopt(argc, argv, options)
  51. int argc;
  52. char *argv[];
  53. struct long_option *options;
  54.     {
  55.     if (not_an_option == optind)
  56.         return -1;
  57.     if (optind >= argc)
  58.         return -1;
  59.     if (argv[optind][0] == '-')
  60.         {
  61.         char *match = argv[optind]+1;
  62.         if (strlen(match) == 1)
  63.             {
  64.             if (match[0] == '-')
  65.                 {
  66.                 not_an_option = ++optind;
  67.                 return -1;
  68.                 }
  69.             while(options->fulltext)
  70.                 {
  71.                 if (options->abbrev == match[0])
  72.                     return parse_option(argv, options);
  73.                 else
  74.                     options++;
  75.                 }
  76.             return -1;
  77.             }
  78.         else
  79.             {
  80.             int max_match = 0;
  81.             struct long_option *best = 0;
  82.  
  83.             while (options->fulltext)
  84.                 {
  85.                 int i;
  86.                 for (i = 0; ; i++)
  87.                     {
  88.                     if (options->fulltext[i] == 0 && match[i] == 0)
  89.                         return parse_option(argv, options);
  90.                     if (match[i] == 0)
  91.                         {
  92.                         if (i > max_match)
  93.                             {
  94.                             max_match = i;
  95.                             best = options;
  96.                             }
  97.                         break;
  98.                         }
  99.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  100.                         break;
  101.                     }
  102.                 options++;
  103.                 }
  104.             if (max_match < 3)
  105.                 {
  106.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  107.                 return -1;
  108.                 }
  109.             return parse_option(argv, best);
  110.             }
  111.         }
  112.     else
  113.         return -1;
  114.     }
  115.